package com.book.controller;

import com.book.model.entity.Address;
import com.book.service.AddressService;
import com.book.util.BaseResult;
import com.book.util.QiniuUtil;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 * 地址信息 前端控制器
 * </p>
 *
 * @author auto
 * @since 2025-04-02 06:38:55
 */
@RestController
@RequestMapping("/address")
@Tag(name = "地址信息", description = "地址信息相关接口")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @Autowired
    private QiniuUtil qiniuUtil;
    @ApiOperation(value = "查询用户的收货地址")
    @GetMapping("/list/{userId}")
    public BaseResult<?> getAddressList(@PathVariable("userId") String userId) {
        return BaseResult.successResult(addressService.getAddressListByUserId(userId));
    }

    @ApiOperation(value = "新增收货地址")
    @PostMapping("/")
    public BaseResult<?> addAddress(@RequestBody Address address) {
        return BaseResult.successResult(addressService.saveAddress(address));
    }

//    @ApiOperation(value = "修改收货地址")
//    @PutMapping("/")
//    public BaseResult<?> updateAddress(@RequestBody Address address) {
//        return BaseResult.successResult(addressService.updateAddress(address));
//    }

    @ApiOperation(value = "删除收货地址")
    @DeleteMapping("/{id}")
    public BaseResult<?> deleteAddress(@PathVariable("id") String id) {
        return BaseResult.successResult(addressService.removeById(id));
    }


    @ApiOperation(value = "上传文件到七牛云")
    @PostMapping("/upload")
    public BaseResult<?> uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            String fileUrl = qiniuUtil.uploadFile(file);
            if (fileUrl != null) {
                return BaseResult.successResult(fileUrl);
            } else {
                return BaseResult.errorResult("文件上传失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return BaseResult.errorResult("文件上传失败");
        }
    }
}
